package edu.ilstu.mind.ise.ccp.impl.disruptor;

import com.lmax.disruptor.RingBuffer;

import edu.ilstu.mind.ise.ccp.message.Message;
import edu.ilstu.mind.ise.ccp.message.MessageSender;

class DisruptorMessageSender implements MessageSender {
	private final RingBuffer<MessageEvent> ringBuffer;
	
	public DisruptorMessageSender(RingBuffer<MessageEvent> ringBuffer) {
		this.ringBuffer = ringBuffer;
	}
	
	@Override
	public void sendMessage(Message message) {
		// reserve spot in ring buffer
		long sequence = ringBuffer.next();
		// get event in ring buffer for given sequence
		MessageEvent event = ringBuffer.get(sequence);
		// populate event
		event.message = message;
		event.process = true;
		// publish event on ring buffer
		ringBuffer.publish(sequence);
	}
}
